package May;

import java.util.Random;
import java.util.Scanner;

public class Test10ArrayUsing03 {
    public static void main(String[] args) {
        int[] arr = new int[5];
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入5个数字，我将会把它们加入到一个数组之中，并完成接下来的操作");

        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
//        for (int temp:arr){
//            System.out.print(temp+" ");
//        }

        format(arr,"您输入的数字原来的序列是:");

        for (int i = 0; i < arr.length; i++) {
            int randomNumber = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[randomNumber];
            arr[randomNumber] = temp;
        }

        format(arr,"数字已经被打乱：");

        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            int temp2 = arr[i];
            arr[i] = arr[j];
            arr[j] = temp2;
        }


        format(arr,"接下来我将会把打乱后的数字倒序排列：");

        bubbleSort(arr);
    }
    public static void bubbleSort(int[] arr_b){
        for (int i = 0; i < arr_b.length-1; i++) {
            for (int j = 0; j < arr_b.length-i-1; j++) {
                if (arr_b[j]>arr_b[j+1]){
                    int temp=arr_b[j];
                    arr_b[j]=arr_b[j+1];
                    arr_b[j+1]=temp;
                }
            }
        }
        format(arr_b,"从小到大冒泡排序后的数组为：");
    }
    //将格式化写成方法
    public static void format(int[] arr_f,String usage){
        System.out.println(usage);
        for (int i = 0; i < arr_f.length; i++) {
            if (i<arr_f.length-1){
                System.out.print(arr_f[i]+" ");
            }else {
                System.out.println(arr_f[i]);
            }
        }
    }
}
